package com.producteev4j.services.version0;

import com.producteev4j.exceptions.ProducteevException;
import com.producteev4j.model.json.user.UserListWrapperImpl;
import com.producteev4j.model.json.user.UserLoginWrapperImpl;
import com.producteev4j.model.json.user.UserWrapperImpl;
import com.producteev4j.model.request.ProducteevGetRequest;
import com.producteev4j.model.request.ProducteevPostRequest;
import com.producteev4j.model.request.ProducteevRequest;
import com.producteev4j.model.response.User;
import com.producteev4j.model.response.UserLogin;
import com.producteev4j.model.response.UserWrapper;
import com.producteev4j.transport.ProducteevTransport;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jcarrey
 * Date: 16/05/11
 * Time: 22:32
 * To change this template use File | Settings | File Templates.
 */
public class ProducteevUserServiceV0Impl implements ProducteevUserService {

    // private static final Logger logger = Logger.getLogger(ProducteevServiceV0.class);

    //  USER SERVICES

    private static final String USER_CONTEXT_PATH = "/users";

    private static final String USER_VIEW_SERVICE = "/view.json";
    // user login token

    private static final String USER_LOGIN_SERVICE = "/login.json";
    // username + password
    // 401 - Failed

    private static final String USER_LOGIN_FB_SERVICE = "/fblogin.json";
    // Params: fb_uid, fb_access_token (Facebook oAuth access token)
    // 401 - Failed

    private static final String USER_SET_DEFAULT_DASHBOARD_SERVICE = "/set_default_dashboard.json";
    // id_dashboard
    private static final String USER_COLLEAGUES_SERVICE = "/colleagues.json";
    // user login token
    private static final String USER_SIGNUP_SERVICE = "/signup.json";
    // email, firstname, lastname, password, String timezone (http://code.google.com/p/producteev-api/wiki/TimezonesList), fbuid (optional)
    private static final String USER_ATTACH_FB_ID_SERVICE = "/attach_facebook_id.json";
    // fb_uid
    private static final String USER_DETACH_FB_ID_SERVICE = "/detach_facebook_id.json";
    // fb_uid
    private static final String USER_SET_SORT_BY_SERVICE = "/set_sort_by.json";
    // int reminder =
    // by deadline = 1, by manager = 2, by labels = 3, by time_lastchange = 4, by workspace = 5, by priority = 6, by title = 7, by time_created = 8)
    private static final String USER_SET_TIMEZONE_SERVICE = "/set_timezone.json";

    // ENDPOINTS

    private static final String USER_LOGIN_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_LOGIN_SERVICE;
    private static final String USER_VIEW_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_VIEW_SERVICE;
    private static final String USER_LOGIN_FB_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_LOGIN_FB_SERVICE;
    private static final String USER_SET_DEFAULT_DASHBOARD_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_SET_DEFAULT_DASHBOARD_SERVICE;
    private static final String USER_COLLEAGUES_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_COLLEAGUES_SERVICE;
    private static final String USER_SIGNUP_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_SIGNUP_SERVICE;
    private static final String USER_ATTACH_FB_ID_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_ATTACH_FB_ID_SERVICE;
    private static final String USER_DETACH_FB_ID_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_DETACH_FB_ID_SERVICE;
    private static final String USER_SET_SORT_BY_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_SET_SORT_BY_SERVICE;
    private static final String USER_SET_TIMEZONE_ENDPOINT = ProducteevServiceV0.PRODUCTEEV_API_ENDPOINT + USER_CONTEXT_PATH + USER_SET_TIMEZONE_SERVICE;


    // PARAMS
    private static final String EMAIL_PARAM = "email";

    private static final String PASSWORD_PARAM = "password";

    private static final String ID_COLLEAGUE_PARAM = "id_colleague";

    private static final String FB_UID = "fb_uid";

    private static final String FB_ACCESSS_TOKEN = "fb_access_token";

    private static final String FIRST_NAME_PARAM = "firstname";

    private static final String LAST_NAME_PARAM = "lastname";

    private static final String TIMEZONE_PARAM = "timezone";

    private static final String ID_DASHBOARD_PARAM = "id_dashboard";

    private static final String REMINDER_PARAM = "reminder";

    private static final String SORT_PARAM = "sort";


    public UserLogin userLoginFacebook(ProducteevTransport transport, long facebookId, String facebookAccessToken) throws ProducteevException {
        if (facebookAccessToken == null) {
            throw new IllegalArgumentException("facebookAccessToken is mandatory");
        }

        ProducteevRequest request = new ProducteevGetRequest();
        request.getParams().putValue(FB_UID, facebookId);
        request.getParams().putValue(FB_ACCESSS_TOKEN, facebookAccessToken);
        request.setResponseClass(UserLoginWrapperImpl.class);
        request.setEndpoint(USER_LOGIN_FB_ENDPOINT);
        return ProducteevValidator.assertNoError((UserLoginWrapperImpl) transport.process(request)).getLogin();
    }

    public UserLogin userLogin(ProducteevTransport transport, String email, String password) throws ProducteevException {
        /*
           if (logger.isTraceEnabled()) {
            logger.trace("User login:" + email + " password : ***** ");
           }
        */

        ProducteevRequest request = new ProducteevGetRequest();
        request.getParams().putValue(EMAIL_PARAM, email);
        request.getParams().putValue(PASSWORD_PARAM, password);
        request.setEndpoint(USER_LOGIN_ENDPOINT);
        request.setResponseClass(UserLoginWrapperImpl.class);


        return ProducteevValidator.assertNoError((UserLoginWrapperImpl) transport.process(request)).getLogin();

    }


    public User getUser(ProducteevTransport transport, String userToken, String idCollegue) throws ProducteevException {
        if (userToken == null) {
            throw new IllegalArgumentException("userToken is mandatory");
        }
        ProducteevRequest request = new ProducteevGetRequest();
        if (idCollegue != null) {
            request.getParams().putValue(ID_COLLEAGUE_PARAM, idCollegue);
        }

        request.setResponseClass(UserWrapperImpl.class);
        request.setEndpoint(USER_VIEW_ENDPOINT);


        return ProducteevValidator.assertNoError((UserWrapperImpl) transport.process(request, userToken)).getUser();
    }


    public User setDefaultDashboard(ProducteevTransport transport, String userToken, long idDashboard) throws ProducteevException {
        if (userToken == null) {
            throw new IllegalArgumentException("userToken is mandatory");
        }

        ProducteevRequest request = new ProducteevGetRequest();
        request.setResponseClass(UserWrapperImpl.class);
        request.setEndpoint(USER_SET_DEFAULT_DASHBOARD_ENDPOINT);
        request.getParams().putValue(ID_DASHBOARD_PARAM, idDashboard);


        return ProducteevValidator.assertNoError((UserWrapperImpl) transport.process(request, userToken)).getUser();
    }


    public List<? extends UserWrapper> getColleagues(ProducteevTransport transport, String userToken) throws ProducteevException {
        if (userToken == null) {
            throw new IllegalArgumentException("userToken is mandatory");
        }

        ProducteevRequest request = new ProducteevGetRequest();
        request.setResponseClass(UserListWrapperImpl.class);
        request.setEndpoint(USER_COLLEAGUES_ENDPOINT);

        return ProducteevValidator.assertNoError((UserListWrapperImpl) transport.process(request, userToken)).getColleagues();
    }


    public User signupUser(ProducteevTransport transport,
                           String firstName, String lastName, String password, String email, String timeZone, Long fbuid) throws ProducteevException {

        // TODO validate params

        ProducteevRequest request = new ProducteevPostRequest();
        request.setResponseClass(UserWrapperImpl.class);
        request.setEndpoint(USER_SIGNUP_ENDPOINT);
        request.getParams().putValue(FIRST_NAME_PARAM, firstName);
        request.getParams().putValue(LAST_NAME_PARAM, lastName);
        request.getParams().putValue(PASSWORD_PARAM, password);
        request.getParams().putValue(EMAIL_PARAM, email);
        request.getParams().putValue(TIMEZONE_PARAM, timeZone);

        if (fbuid != null) {
            request.getParams().putValue(FB_UID, fbuid);
        }


        return ProducteevValidator.assertNoError((UserWrapperImpl) transport.process(request)).getUser();
    }


    public User setSortByType(ProducteevTransport transport, String userToken, int sortByType) throws ProducteevException {

        // TODO validate params

        ProducteevRequest request = new ProducteevGetRequest();
        request.setResponseClass(UserWrapperImpl.class);
        request.setEndpoint(USER_SET_SORT_BY_ENDPOINT);
        request.getParams().putValue(SORT_PARAM, sortByType);

        return ProducteevValidator.assertNoError((UserWrapperImpl) transport.process(request, userToken)).getUser();
    }


    // TODO attach facebookId / detach facebookId / setTimezone

}
